package com.ruoyi.baoziyi.service.imp;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.baoziyi.domain.SerBaoziyiDeviceInfo;
import com.ruoyi.baoziyi.mapper.SerBaoziyiDeviceInfoMapper;
import com.ruoyi.baoziyi.service.BZYDeviceService;
import com.ruoyi.baoziyi.utils.FileUtil;
import com.ruoyi.permission.Dao.SerDevicePermi;
import com.ruoyi.permission.mapper.SerDevicePermiMapper;
import com.ruoyi.utils.LocationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BZYDeviceServiceImpl implements BZYDeviceService {

    @Autowired
    private SerBaoziyiDeviceInfoMapper  baoziyiDeviceInfoMapper;
    @Autowired
    private SerDevicePermiMapper permiMapper;

//    private static String folder_path = "E:\\temp\\zhibao\\Spore_pic";
//    private static String folder_path = "/home/xingyou_ftp/zhibao/Spore_pic";
    @Value("${ruoyi.BZYProfile}")
    private String folder_path;
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int getBZYDeviceInfo(){
        //调用方法来获取文件夹名称列表
        List<String> folderNames = FileUtil.listSubfolders(folder_path);
        int i = 0;
        //打印文件夹名称
        for(String folderName:folderNames){
            SerBaoziyiDeviceInfo serBaoziyiDeviceInfo = new SerBaoziyiDeviceInfo().setDeviceId(folderName).setUpdateTime(SDF.format(new Date()));
            if(baoziyiDeviceInfoMapper.selectByPrimaryKey(folderName) != null){
                i = i + baoziyiDeviceInfoMapper.updateByPrimaryKeySelective(serBaoziyiDeviceInfo);
            }else {
                i = i + baoziyiDeviceInfoMapper.insert(serBaoziyiDeviceInfo);
            }
        }
        return i;
    }

    @Override
    public List<SerBaoziyiDeviceInfo> selectAllBZYDeviceInfo(){
        return baoziyiDeviceInfoMapper.selectAllDevice();
    }

    @Override
    public List<SerBaoziyiDeviceInfo> selectAllBZYDeviceInfoByCondition(List<SerDevicePermi> serDevicePermiList){
        List<SerBaoziyiDeviceInfo> allBaoziyiDeviceInfoList = baoziyiDeviceInfoMapper.selectAllDevice();
        if(!serDevicePermiList.isEmpty()){
            allBaoziyiDeviceInfoList = allBaoziyiDeviceInfoList.stream()
                    .filter(filteredBaoZiYiDeviceInfo -> serDevicePermiList.stream()
                            .anyMatch(serDevicePermi -> serDevicePermi.getDeviceSerial().equals(filteredBaoZiYiDeviceInfo.getDeviceId())))
                    .collect(Collectors.toList());
        }
        return allBaoziyiDeviceInfoList;
    }
    @Override
    public SerBaoziyiDeviceInfo selectBZYDeviceByCondition(SerBaoziyiDeviceInfo serBaoziyiDeviceInfo){
        return baoziyiDeviceInfoMapper.selectByCondition(serBaoziyiDeviceInfo);
    }

    @Override
    public int updateBZYDeviceInfo(SerBaoziyiDeviceInfo serBaoziyiDeviceInfo) throws IOException {
        String deviceId = serBaoziyiDeviceInfo.getDeviceId();
        String nickName = null;
        String manualId = null;
        Double longitudeFinal = null;
        Double latitudeFinal = null;
        String locationFinal = null;
        String areaFinal = null;
        String provinceFinal = null;
        if(serBaoziyiDeviceInfo.getLocation()!=null && serBaoziyiDeviceInfo.getLocation()!=""){
            locationFinal = serBaoziyiDeviceInfo.getLocation();
            double[] location = LocationUtil.geocode(serBaoziyiDeviceInfo.getLocation());
            longitudeFinal = location[0];
            latitudeFinal = location[1];
//            serBaoziyiDeviceInfo.setLongitude(longitudeFinal);
//            serBaoziyiDeviceInfo.setLatitude(latitudeFinal);
            JSONObject locationObject = LocationUtil.reverseGeoCode(longitudeFinal,latitudeFinal);
            //详细地址，用于更新各个表中的地址
//            String locationStr = locationObject.getString("formattedAddress");
            areaFinal = locationObject.getString("city") + locationObject.get("district");
            provinceFinal = locationObject.getString("province");
        }
        if(serBaoziyiDeviceInfo.getNickName()!=null && serBaoziyiDeviceInfo.getNickName()!=""){
            nickName = serBaoziyiDeviceInfo.getNickName();
        }
        if(serBaoziyiDeviceInfo.getManualId()!=null && serBaoziyiDeviceInfo.getManualId()!=""){
            manualId = serBaoziyiDeviceInfo.getManualId();
        }
        serBaoziyiDeviceInfo.setLongitude(longitudeFinal).setLatitude(latitudeFinal).setLocation(locationFinal);
        int i = baoziyiDeviceInfoMapper.updateByPrimaryKeySelective(serBaoziyiDeviceInfo);
        SerDevicePermi serDevicePermi = new SerDevicePermi().setDeviceSerial(deviceId).setDeviceName(nickName).setManualId(manualId)
                .setLongitude(longitudeFinal).setLatitude(latitudeFinal).setArea(areaFinal).setProvince(provinceFinal);
        int j = permiMapper.updateByPrimaryKeySelective(serDevicePermi);
        return i+j;
    }





}
